package zone.czh.woi.woim.client.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import lombok.AllArgsConstructor;
import zone.czh.woi.base.util.Pair;
import zone.czh.woi.protocol.protocol.Payload;
import zone.czh.woi.woim.client.WOIMClient;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
*@ClassName: ReqHandler
*@Description: None
*@author woi
*/
@AllArgsConstructor
public class ReqHandler extends MessageToMessageEncoder<WOIMClient.Req> {
    private WOIMClient client;
    @Override
    protected void encode(ChannelHandlerContext ctx, WOIMClient.Req req, List<Object> out) throws Exception {
        Payload payload = req.getPayload();
        if (payload.isResponseRequired()){
            if (req.getResponseHandler()!=null){
                client.getResponseHandlers().put(payload.getSn(),new Pair(req.getResponseHandler(),req.isBlock()));
            }

            //无论有没有超时处理器，超时都得尝试取出响应处理器，否则会内存泄漏
            ctx.channel().eventLoop().schedule(() -> {
                Pair<WOIMClient.ResponseHandler, Boolean> responseHandler = client.getResponseHandlers().remove(payload.getSn());
                if (responseHandler!=null){//即响应处理器并没有被取出执行
                    WOIMClient.TimeoutHandler timeoutHandler = req.getTimeoutHandler();
                    if (timeoutHandler!=null){
                        timeoutHandler.handleTimeout();
                    }
                }

            }, req.getExpiration(), TimeUnit.MILLISECONDS);
        }
        out.add(payload);
    }

}
